<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Ext_detail_penawaran extends MY_Controller {
	
	function __construct() {
		parent::__construct(); 
		
		$this->load->model('ext_detail_penawaran_model', 'DPenawaran');
		$this->load->model('ext_combo_model', 'Combo');
		$this->secure_session();
	}

	function getdata($id_penawaran) {
		$this->secure_session();
		if ($id_penawaran != "") {
			echo $this->DPenawaran->GetJSON($id_penawaran);
		}
	}

	function ajax_post($id_penawaran) {
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";



		$error = $this->validation();
		try {
			if (count( $error->Get() ) > 0) { 
				$is_error = 1; 
			} else {
				// Fill Post Data
				$data	= post_by_key(array('id_detail_penawaran_pk', 'id_penawaran_fk', 'id_barang_fk','id_satuan_fk','harga_satuan', 'totalharga', 'jumlah_barang', 'tgl_dibutuhkan','ket_detail_penawaran'));
				$data['id_detail_penawaran_pk']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['id_detail_penawaran_pk'])));
				// Set Date Format
				 $data['tgl_dibutuhkan']	= ($data['tgl_dibutuhkan'] == '' ? NULL : date_format3($data['tgl_dibutuhkan'], 'YYYY-MM-DD'));
				$data['totalharga']	= (float)toNumber($data['totalharga']);
				$data['harga_satuan']	= (float)toNumber($data['harga_satuan']);
				$data['jumlah_barang']	= (float)toNumber($data['jumlah_barang']);



				$data['id_penawaran_fk'] = $id_penawaran;
				
				if (post("mode") == "create") {
					$data['tgl_rekam']	= NOW;
					$data['user_rekam']	= USERID;
					$data['id_detail_penawaran_pk'] = $this->DPenawaran->GenerateId();

					$this->DPenawaran->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.app_id ?? ""));

				} else if (post("mode") == "edit") {
					$data['tgl_ubah']	= NOW;
					$data['user_ubah']	= USERID;
					
					$this->DPenawaran->Update($data, 
										array('id_detail_penawaran_pk'	=> $data['id_detail_penawaran_pk']));

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS002", "edit/" + (data.app_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	private function validation() {
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");

		if (post("harga_satuan") == '') {
			$error->Add("dtl_penawaran_harga_satuan", "Harga Barang", $tpl_empty);
		}
		if (post("tgl_dibutuhkan") == '') {
			$error->Add("dtl_penawaran_tgl_dibutuhkan", "Tanggal Dibutuhkan", $tpl_empty);
		} 
		if (post("id_satuan_fk") == '') {
			$error->Add("dtl_penawaran_id_satuan_fk", "ID satuan", $tpl_empty);
		} 
		if (post("id_barang_fk") == '') {
			$error->Add("dtl_penawaran_id_barang_fk", "ID Barang", $tpl_empty);
		} 

		return $error;
	}

	function ajax_delete() {
		$ls_result	= "";
		$id_detail_penawaran_pk	= get('id_detail_penawaran_pk');
		
		if ($id_detail_penawaran_pk != "") {
			// Delete Single
			$arr_rule	= array('id_detail_penawaran_pk' => $id_detail_penawaran_pk);
			$ls_result = $this->DPenawaran->DeleteSingle($arr_rule);

		} else {
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++) {
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_detail_penawaran_pk' => $row['id_detail_penawaran_pk']);
				$ls_result .= $this->DPenawaran->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}

}
?>